VERSION 5.00
Object = "{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0"; "mscomctl.OCX"
Object = "{48E59290-9880-11CF-9754-00AA00C00908}#1.0#0"; "MSINET.Ocx"
Begin VB.Form frmLiveupdate 
   BorderStyle     =   1  'Fixed Single
   Caption         =   "Live update"
   ClientHeight    =   2580
   ClientLeft      =   45
   ClientTop       =   435
   ClientWidth     =   6240
   LinkTopic       =   "Form1"
   MaxButton       =   0   'False
   MinButton       =   0   'False
   ScaleHeight     =   2580
   ScaleWidth      =   6240
   StartUpPosition =   2  'CenterScreen
   Begin VB.TextBox Text2 
      Height          =   495
      Left            =   2640
      TabIndex        =   12
      Text            =   "Text2"
      Top             =   7080
      Width           =   2535
   End
   Begin VB.TextBox Text1 
      Height          =   375
      Left            =   2640
      TabIndex        =   11
      Text            =   "Text1"
      Top             =   6480
      Width           =   2535
   End
   Begin VB.TextBox rtbDocument 
      Height          =   285
      Left            =   6960
      TabIndex        =   10
      Text            =   "Text1"
      Top             =   6960
      Width           =   615
   End
   Begin InetCtlsObjects.Inet Inet1 
      Left            =   5040
      Top             =   1320
      _ExtentX        =   1005
      _ExtentY        =   1005
      _Version        =   393216
   End
   Begin VB.CommandButton Command3 
      Caption         =   "Get online version info"
      Height          =   495
      Left            =   4200
      TabIndex        =   4
      Top             =   1920
      Visible         =   0   'False
      Width           =   1935
   End
   Begin MSComctlLib.ProgressBar ProgressBar1 
      Height          =   375
      Left            =   1200
      TabIndex        =   2
      Top             =   120
      Width           =   4935
      _ExtentX        =   8705
      _ExtentY        =   661
      _Version        =   393216
      Appearance      =   1
   End
   Begin VB.CommandButton Command1 
      Caption         =   "Cancel"
      Height          =   495
      Left            =   4920
      TabIndex        =   1
      Top             =   600
      Width           =   1215
   End
   Begin VB.CommandButton Command2 
      Caption         =   "Download && Install"
      BeginProperty Font 
         Name            =   "MS Sans Serif"
         Size            =   8.25
         Charset         =   0
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   495
      Left            =   2880
      TabIndex        =   0
      Top             =   600
      Width           =   1935
   End
   Begin VB.Timer Timer1 
      Enabled         =   0   'False
      Interval        =   1000
      Left            =   3840
      Top             =   1320
   End
   Begin VB.Label Label1 
      Caption         =   "Label2"
      Height          =   615
      Left            =   120
      TabIndex        =   13
      Top             =   1920
      Width           =   6015
   End
   Begin VB.Label lblProgressInfo 
      Height          =   255
      Left            =   120
      TabIndex        =   9
      Top             =   2040
      Width           =   3735
   End
   Begin VB.Line Line1 
      X1              =   6120
      X2              =   0
      Y1              =   1200
      Y2              =   1200
   End
   Begin VB.Label lblOnlineRundate 
      BackStyle       =   0  'Transparent
      Caption         =   "Online Version Rundate"
      Height          =   375
      Left            =   120
      TabIndex        =   8
      Top             =   1560
      Width           =   4095
   End
   Begin VB.Label lblOnlineVer 
      BackStyle       =   0  'Transparent
      Caption         =   "Online verion is "
      Height          =   255
      Left            =   120
      TabIndex        =   7
      Top             =   1320
      Width           =   3975
   End
   Begin VB.Label lblRundate 
      Caption         =   "Rundate"
      Height          =   375
      Left            =   120
      TabIndex        =   6
      Top             =   840
      Width           =   2655
   End
   Begin VB.Label lblCurrVer 
      Caption         =   "Current ver "
      Height          =   255
      Left            =   120
      TabIndex        =   5
      Top             =   600
      Width           =   2415
   End
   Begin VB.Label Label11 
      Caption         =   "Downloading"
      Height          =   495
      Left            =   120
      TabIndex        =   3
      Top             =   120
      Width           =   1215
   End
End
Attribute VB_Name = "frmLiveupdate"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Private m_lngDocSize As Long
Const INTERNET_OPEN_TYPE_PRECONFIG = 0
Const INTERNET_FLAG_EXISTING_CONNECT = &H20000000

Private Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" _
    (ByVal lpszAgent As String, ByVal dwAccessType As Long, _
    ByVal lpszProxyName As String, ByVal lpszProxyBypass As String, _
    ByVal dwFlags As Long) As Long
Private Declare Function InternetOpenUrl Lib "wininet.dll" Alias _
    "InternetOpenUrlA" (ByVal hInternetSession As Long, ByVal lpszUrl As String, _
    ByVal lpszHeaders As String, ByVal dwHeadersLength As Long, _
    ByVal dwFlags As Long, ByVal dwContext As Long) As Long
Private Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet As _
    Long) As Integer
Private Declare Function InternetReadFile Lib "wininet.dll" (ByVal hFile As _
    Long, ByVal lpBuffer As String, ByVal dwNumberOfBytesToRead As Long, _
    lNumberOfBytesRead As Long) As Integer
'''''Dim bytes() As Byte
'''''Dim lngSize As Long
Dim strURL  As String
Dim strURLOnlineVersionInfo As String

Dim dot1 As Integer
Dim dot2 As Integer
Dim path_gaaupdate As String 'path of file gaaupdate.exe

Private Sub Command1_Click() 'Cancel
    Inet1.Cancel
    End
End Sub

Private Sub Command2_Click() 'Download
    If major < majorOnline Then
        Call updatelastVersion
    ElseIf minor < minorOnline Then
        Call updatelastVersion
    ElseIf revision < revisionOnline Then
        Call updatelastVersion
    Else
        Label1.ForeColor = &HFF&
        Label1.Caption = "You are currently running the latest Galileo Accountant version " & currVer
        Exit Sub
    End If
End Sub

Sub updatelastVersion()
    Inet1.Cancel
    
    Dim bData() As Byte
    Screen.MousePointer = vbHourglass

    FileSystem.ChDir App.Path
    Inet1.Protocol = icHTTP
'''    Command1.Enabled = True
    'start download
    Do While Inet1.StillExecuting = True
        DoEvents
    Loop
    Inet1.Execute Trim$(strURL), "GET"
'''    Command1.Visible = True
'''    'Timer1.Enabled = True
'''    Command2.Visible = False
    Screen.MousePointer = vbDefault
    Beep
End Sub
 

Private Sub Command3_Click() 'Get online version info
    Label1.ForeColor = &H80000012
    Inet1.Cancel
    If Inet1.StillExecuting Then Exit Sub
    
    Dim dByte() As Byte
    Screen.MousePointer = vbHourglass
'''    Command1.Visible = True
'''    Command2.Visible = False
    Label1.Caption = "Get online version info is running.."
    dByte = Inet1.OpenURL(strURLOnlineVersionInfo, icByteArray)
    Open App.Path & "\versioninfo.htm" For Binary As #1
        Put #1, , dByte()
    Close #1
    While Inet1.StillExecuting
        Label1.Caption = "Running.."
    Wend
    Label1.Caption = "Download Status"
    Inet1.Cancel
    lblOnlineVer.Caption = "Online version is " & GetOnlineCurrentVersion
    lblOnlineRundate.Caption = "Online version " & GetOnlineCurrentRundateVersion
    Screen.MousePointer = vbDefault
'''    Command1.Visible = False
'''    Command2.Visible = True
    
    If major < majorOnline Then
        Label1.ForeColor = &HFF&
        Label1.Caption = "You are currently running Galileo Accountant " & currVer & ". A newer version, Galileo Accountant " & strOnlineVer & " is now available"
    ElseIf minor < minorOnline Then
        Label1.ForeColor = &HFF&
        Label1.Caption = "You are currently running Galileo Accountant " & currVer & ". A newer version, Galileo Accountant " & strOnlineVer & " is now available"
    ElseIf revision < revisionOnline Then
        Label1.ForeColor = &HFF&
        Label1.Caption = "You are currently running Galileo Accountant " & currVer & ". A newer version, Galileo Accountant " & strOnlineVer & " is now available"
    Else
        Label1.Caption = "You are currently running the latest Galileo Accountant version " & currVer
        Exit Sub
    End If
End Sub

Private Sub Form_Load()
    Me.Top = Screen.Height / 2 - Me.Height / 2
    Me.Left = Screen.Width / 2 - Me.Width / 2
    strURLOnlineVersionInfo = "http://www.galileo.com.vn/down/liveupdate/versioninfo.htm"
    strURL = "http://www.galileo.com.vn/down/liveupdate/gaaupdate.exe"

    'Timer1.Enabled = False
'''    Command2.Visible = True
'''    Command1.Visible = False
    lblCurrVer.Caption = "Current version is " & GetCurrentVersion
    lblRundate.Caption = "Rundate " & GetCurrentVersionDate
    'initialize and stage controls
    ScaleMode = vbPixels
    FileSystem.ChDir App.Path
    path_gaaupdate = App.Path & "\gaaupdate.exe"
    ProgressBar1.Max = 100
    ' Command1.Caption = "Download"
    Label1.Caption = "Download Status"
    Me.Caption = "Updating Galileo Accountant"
    ''''Me.Caption = "Liveupdate ver" & liveUpdateVersion & " Compiledate " & LiveUpdateCompileDate
    Command3_Click
End Sub

Public Function WriteCurrentVersionToFile(currVer As String)
    Open App.Path & "\CurrVer.dat" For Output As #1
        Print #1, currVer
    Close #1
End Function

Public Function GetCurrentVersionDate() As String
    Dim currVerDate As String
    Open App.Path & "\CurrVer.dat" For Input As #1
        Line Input #1, currVerDate
        Line Input #1, currVerDate
    Close #1
    GetCurrentVersionDate = currVerDate
End Function

Public Function GetCurrentVersion() As String
    Dim mids As String 'string between dot1 and dot2 of currVer
    
    If Dir(App.Path & "\CurrVer.dat") = "" Then
        Exit Function
    End If
    Open App.Path & "\CurrVer.dat" For Input As #1
        Line Input #1, currVer
    Close #1
    
    dot1 = InStr(currVer, ".")
    dot2 = InStr((InStr(currVer, ".") + 1), currVer, ".")
    major = Left(currVer, dot1 - 1)
    mids = Mid(currVer, dot1 + 1)
    minor = Left(mids, InStr(mids, ".") - 1)
    revision = Mid(currVer, dot2 + 1)
    GetCurrentVersion = currVer
End Function

Public Function GetOnlineCurrentRundateVersion() As String
    Dim strAllData As String
    Dim strOnlineVer As String
    strOnlineVer = ""
    Open App.Path & "\versioninfo.htm" For Input As #1
        Do While Not EOF(1)
            Input #1, strAllData
            If InStr(1, strAllData, "Rundate", vbTextCompare) > 0 Then
                strOnlineVer = Right(strAllData, 1 + Len(strAllData) - InStr(1, strAllData, "Rundate", vbTextCompare))
            End If
        Loop
    Close #1
    GetOnlineCurrentRundateVersion = strOnlineVer
End Function

Public Function GetOnlineCurrentVersion() As String
    Dim strAllData As String
    Dim mids As String
    strOnlineVer = ""
    Open App.Path & "\versioninfo.htm" For Input As #1
        Do While Not EOF(1)
            Input #1, strAllData
            If InStr(1, strAllData, "version", vbTextCompare) > 0 Then
                'strOnlineVer = Right(strAllData, 1 + Len(strAllData) - InStr(1, strAllData, "version", vbTextCompare))
                strOnlineVer = Mid(strAllData, InStr(strAllData, "is ") + 3)
            End If
        Loop
    Close #1
    
    dot1 = InStr(strOnlineVer, ".")
    dot2 = InStr((InStr(strOnlineVer, ".") + 1), strOnlineVer, ".")
    majorOnline = Left(strOnlineVer, dot1 - 1)
    mids = Mid(strOnlineVer, dot1 + 1)
    minorOnline = Left(mids, InStr(mids, ".") - 1)
    revisionOnline = Mid(strOnlineVer, dot2 + 1)
    
    GetOnlineCurrentVersion = strOnlineVer
End Function

Private Sub Inet1_StateChanged(ByVal State As Integer)
    Dim Buffer() As Byte 'buffer for receiving data from inet control.
    On Error Resume Next
    Select Case State
        Case icResponseCompleted
            Open path_gaaupdate For Binary As #1 'open output file
                Do
                    DoEvents
                    Buffer = Inet1.GetChunk(512, icByteArray) 'get the data chunk
                    If UBound(Buffer) = -1 Then Exit Do 'no more data
                    Put #1, , Buffer    'write data to file
                    ProgressBar1.Value = Seek(1) - 1 'ammount of data downloaded
                    Label1.Caption = "Downloaded " & ProgressBar1.Value & " bytes, " & _
                    FormatPercent(ProgressBar1.Value / ProgressBar1.Max)
                Loop
            Close
            'Picture = LoadPicture(Text2.Text) 'display the downloaded image
            If MsgBox("Download successfully, do you want to upgrade the newest version? (Y/N)", vbQuestion + vbYesNo, "Live update") = vbNo Then
            
            Else
                FileSystem.ChDir App.Path
                Shell path_gaaupdate
                End
            End If
        Case icResponseReceived
            If LenB(Inet1.GetHeader("Content-Length")) > 0 Then
                ProgressBar1.Max = CLng(Inet1.GetHeader("Content-Length")) 'total size
            End If
    End Select
End Sub
 
